package factory;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;

import xml.XMLParse;

import config.Config;

import db.abstract_base.FieldX;

public class FieldFactory {

	static List<FieldX> fieldList;
	static String folderPath="";
	private FieldFactory(){
		fieldList=new ArrayList<FieldX>();
	}
	
	private static FieldFactory instance=new FieldFactory();
	
	public static List<FieldX> createField(String folderpath) throws IOException, ParserConfigurationException, SAXException{
		if(folderpath==FieldFactory.folderPath){
			return fieldList;
		}
		else{
			fieldList.clear();
			
			File fieldDir=new File(Config.getFieldFolder());
			File[] fields= fieldDir.listFiles();
			
			for(int i=0;i<fields.length;i++){
				
				String fieldPath=fields[i].getAbsolutePath();
				XMLParse xmlparse=new XMLParse(fieldPath);
				Map<String, String> valueMap=xmlparse.parse();
				
				String dbType=valueMap.get("FldDBType");
				String internalUsage=valueMap.get("InternalUsage");
				
				if(internalUsage.equals("CPS")){
					switch(dbType){
						case "Bool":
							fieldList.add(BoolFieldFactory.getBoolField(valueMap));
							break;
						default: 
							break;
					}
				}
			}
		}
		return fieldList;
	}
}
